package io.github.yuegod.clean.acl.api.wechat;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import io.github.yuegod.clean.acl.api.Exec;
import io.github.yuegod.clean.acl.model.Result;
import org.springframework.stereotype.Component;

import java.util.Map;

import static io.github.yuegod.clean.acl.enumeration.ActionType.Wechat.*;


/**
 * @author quziwei
 * @date 2021/8/5 9:24
 **/
@Component
public class WechatApi extends WechatApiBase {


    public WechatApi() {
        factory.put(GET_PUBLIC_ACCESS_TOKEN.name(), getPublicAccessToken());
        factory.put(GET_LOGIN_ACCESS_TOKEN.name(), getLoginAccessToken());
        factory.put(GET_USER_INFO.name(),getUserInfo());
        factory.put(REFRESH_TOKEN.name(),refreshToken());
        factory.put(CHECK_TOKEN.name(), checkToken());
    }

    /**
     * 检验Token是否有效
     */
    public Exec checkToken(){
        return e->{
            Map<String, Object> body = e.getBody();
            String url = apiAclClientProperties.getWechat().getWechatApiUrl()+"/sns/auth";
            String res = HttpUtil.get(url,body);
            JSONObject jsonObject = JSONObject.parseObject(res);
            if (jsonObject.getInteger("errorcode") == 0){
                return Result.ok(jsonObject);
            }else {
                return Result.fail(jsonObject.getString("errmsg"));
            }
        };
    }

    /**
     * 刷新Token
     */
    public Exec refreshToken(){
        return e->{
            Map<String, Object> body = e.getBody();
            String url =  apiAclClientProperties.getWechat().getWechatApiUrl()+"/sns/oauth2/refresh_token";
            String res = HttpUtil.get(url,body);
            JSONObject jb = JSONObject.parseObject(res);
            if (jb.get("errorcode") == null){
                return Result.ok(jb);
            }else {
                return Result.fail(jb.getString("errmsg"));
            }
        };
    }

    /**
     * 获取用户信息
     */
    public Exec getUserInfo(){
        return e->{
            Map<String, Object> body = e.getBody();
            String url = apiAclClientProperties.getWechat().getWechatApiUrl()+"/sns/userinfo";
            String res = HttpUtil.get(url, body);
            JSONObject jsonObject = JSONObject.parseObject(res);
            if (jsonObject.get("errorcode") == null){
                return Result.ok(jsonObject);
            }else {
                return Result.fail(jsonObject.getString("errmsg"));
            }
        };
    }

    /**
     * 获取微信登录的AccessToken
     */
    public Exec getLoginAccessToken(){
        return e -> {
            Map<String, Object> body = e.getBody();
            setAppId(body);
            setKey(body);
            String url = apiAclClientProperties.getWechat().getWechatApiUrl() + "/sns/oauth2/access_token";
            String res = HttpUtil.get(url, body);
            JSONObject jsonObject = JSONObject.parseObject(res);
            if (jsonObject.get("errorcode") == null) {
                return Result.ok(jsonObject);
            } else {
                return Result.fail(jsonObject.getString("errmsg"));
            }
        };
    }

    /**
     * 获取微信公众号AccessToken
     */
    public Exec getPublicAccessToken() {
        return e -> {
            Map<String, Object> body = e.getBody();
            String url = apiAclClientProperties.getWechat().getWechatApiUrl() + "/cgi-bin/token";
            body.put("appid", apiAclClientProperties.getWechat().getAppid());
            body.put("secret", apiAclClientProperties.getWechat().getKey());
            String res = HttpUtil.get(url, body);
            JSONObject jsonObject = JSONObject.parseObject(res);
            if (jsonObject.get("errorcode") == null) {
                return Result.ok(jsonObject);
            } else {
                return Result.fail(jsonObject.getString("errmsg"));
            }
        };
    }


}
